﻿using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;

namespace HYRElectric.Calculate
{
	public class MultiHeader : IDisposable
	{
		public MultiHeader(DataGridView grid)
		{
			this.grid = grid;
			grid.CellPainting += this.grid_CellPainting;
			grid.Scroll += this.grid_Scroll;
			this.cellHeight = grid.ColumnHeadersHeight;
		}

		private void grid_Scroll(object sender, ScrollEventArgs e)
		{
			this.grid.Invalidate();
		}

		public TreeNode ColumnTreeNode
		{
			get
			{
				return this.columnRootNode;
			}
			set
			{
				this.columnRootNode = value;
				this.GetNadirColumnNodes(this.columnRootNode);
				this.grid.ColumnHeadersHeight = this.cellHeight * this.columnDeep;
			}
		}

		private void GetNadirColumnNodes(TreeNode node)
		{
			foreach (object obj in node.Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				if (treeNode.Nodes.Count == 0)
				{
					this.columnList.Add(treeNode);
					if (this.columnDeep < treeNode.Level)
					{
						this.columnDeep = treeNode.Level;
					}
				}
				else
				{
					this.GetNadirColumnNodes(treeNode);
				}
			}
		}

		private void grid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
		{
			if (e.RowIndex == -1 && e.ColumnIndex >= 0 && this.columnDeep > 1)
			{
				this.PaintUnitHeader((TreeNode)this.columnList[e.ColumnIndex], e);
				e.Handled = true;
			}
		}

		private void PaintUnitHeader(TreeNode node, DataGridViewCellPaintingEventArgs e)
		{
			if (node.Level == 0)
			{
				return;
			}
			new StringFormat().Alignment = StringAlignment.Center;
			int unitHeaderWidth = this.GetUnitHeaderWidth(node);
			RectangleF rect;
			if (node.Nodes.Count == 0)
			{
				rect = new Rectangle(e.CellBounds.Left, e.CellBounds.Top + (node.Level - 1) * this.cellHeight, unitHeaderWidth - 1, this.cellHeight * (this.columnDeep - node.Level + 1) - 1);
			}
			else
			{
				rect = new Rectangle(e.CellBounds.Left, e.CellBounds.Top + (node.Level - 1) * this.cellHeight, unitHeaderWidth - 1, this.cellHeight - 1);
			}
			if (this.backColorBrush == null)
			{
				this.backColorBrush = new SolidBrush(e.CellStyle.BackColor);
			}
			if (this.gridLinePen == null)
			{
				this.gridLinePen = new Pen(this.grid.GridColor);
			}
			e.Graphics.FillRectangle(this.backColorBrush, rect);
			e.Graphics.DrawLine(this.gridLinePen, rect.Left, rect.Bottom, rect.Right, rect.Bottom);
			e.Graphics.DrawLine(this.gridLinePen, rect.Right, rect.Top, rect.Right, rect.Bottom);
			e.Graphics.DrawString(node.Text, this.grid.Font, Brushes.Black, rect.Left + rect.Width / 2f - e.Graphics.MeasureString(node.Text, this.grid.Font).Width / 2f - 1f, rect.Top + rect.Height / 2f - e.Graphics.MeasureString(node.Text, this.grid.Font).Height / 2f);
			if (node.PrevNode == null && node.Parent != null)
			{
				this.PaintUnitHeader(node.Parent, e);
			}
		}

		private int GetUnitHeaderWidth(TreeNode node)
		{
			int num = 0;
			if (node.Nodes == null)
			{
				return this.grid.Columns[this.GetColumnListNodeIndex(node)].Width;
			}
			if (node.Nodes.Count == 0)
			{
				return this.grid.Columns[this.GetColumnListNodeIndex(node)].Width;
			}
			for (int i = 0; i <= node.Nodes.Count - 1; i++)
			{
				num += this.GetUnitHeaderWidth(node.Nodes[i]);
			}
			return num;
		}

		private int GetColumnListNodeIndex(TreeNode node)
		{
			for (int i = 0; i <= this.columnList.Count - 1; i++)
			{
				if (((TreeNode)this.columnList[i]).Equals(node))
				{
					return i;
				}
			}
			return -1;
		}

		public void Dispose()
		{
			if (this.gridLinePen != null)
			{
				this.gridLinePen.Dispose();
			}
			if (this.backColorBrush != null)
			{
				this.backColorBrush.Dispose();
			}
			this.grid.CellPainting -= this.grid_CellPainting;
			this.grid.Scroll -= this.grid_Scroll;
		}

		private DataGridView grid;

		private int columnDeep = 1;

		private TreeNode columnRootNode;

		private ArrayList columnList = new ArrayList();

		private int cellHeight;

		private SolidBrush backColorBrush;

		private Pen gridLinePen;
	}
}
